/*
*  Copyright (C) 2009  Peter Kist & Jan Ripke
*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "AppContext.h"
#include "Galaxy.h"

#include <exception>

#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>

#include <Util/vfs.h>
#include <Util/Logger.h>

#define APPLICATION_INI_FILE    "application.ini"

namespace galaxy {

    AppPath::AppPath () {
        util::functors::ApplicationName appname;
        fs::path appPath = appname ();

        m_AppName = appPath;
        bin = appPath.parent_path ();
    }


    bool AppPath::loadIniFile () {
        std::string stem = m_AppName.stem() + ".ini";    // get base name of executable (without path or extension)
        fs::path iniFile = bin / stem;

        try {

            std::ifstream ifs(iniFile.string().c_str());
            if (ifs.good ()) {
                boost::archive::xml_iarchive ia(ifs);

                int size;
                ia >> BOOST_SERIALIZATION_NVP(size);

                for (int i = 0; i < size; i++) {
                    AppPathDefinition path;
                    ia >> BOOST_SERIALIZATION_NVP(path);

                    if (path.m_Name == "config") {
                        config = normalizePath (path.m_Path);
                    }
                    if (path.m_Name == "keymaps") {
                        keymaps = normalizePath (path.m_Path);
                    }
                    if (path.m_Name == "logs") {
                        logs = normalizePath (path.m_Path);
                    }
                    if (path.m_Name == "plugins") {
                        plugins = normalizePath (path.m_Path);
                    }

                }
                return true;
			} else {
				std::cerr << "cannot open " << iniFile.string() << std::endl;
			}
        } catch (std::exception& e) {
            LOG_ERROR (std::string ("caught exception while loading ini file:") + e.what ());
        }

        return false;
    }

    bool AppPath::saveIniFile () {
        std::string stem = "saved.ini";    // get base name of executable (without path or extension)
        fs::path iniFile = bin / stem;

        std::ofstream ofs(iniFile.string().c_str());
        if (ofs.good ()) {
            boost::archive::xml_oarchive ia(ofs);

            int size = 2;
            ia << BOOST_SERIALIZATION_NVP(size);

            AppPathDefinition path("config", "$(bin)/../config");
            ia << BOOST_SERIALIZATION_NVP(path);

            path.m_Name = "keymaps";
            path.m_Path = "$(bin)/../config/keymaps";
            ia << BOOST_SERIALIZATION_NVP(path);

            path.m_Name = "logs";
            path.m_Path = "$(bin)/../logs";
            ia << BOOST_SERIALIZATION_NVP(path);

            path.m_Name = "plugins";
            path.m_Path = "$(bin)/plugins_d";
            ia << BOOST_SERIALIZATION_NVP(path);

            return true;
        } 

        return false;
    }

    bool AppPath::validatePaths () {
        if (bin.empty () || config.empty () || keymaps.empty () || logs.empty () || plugins.empty ()) {
            return false;
        }
        return true;
    }

    fs::path AppPath::normalizePath (std::string p) {
        std::string::size_type offset = p.find ("$(bin)");
        if (offset != std::string::npos) {
            p.replace (offset, offset + 6, bin.string ());
        }

        fs::path fullPath (p);
        return fullPath.normalize ();
    }


    AppContext * AppContext::appContext = new AppContext();

    AppContext::AppContext () {
        game = 0L;
    }

} // namespace galaxy
